package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import com.heima.config.OSSAliyunFileStorageService;
import com.heima.pojo.TbSku;
import com.heima.pojo.dto.SkuDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.SkuVo;
import com.heima.service.ITbSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author nan
 * @since 2024-06-06
 */
@RestController
@RequestMapping("/api/vm-service/sku")
@Api(tags = "商品管理")
public class TbSkuController {

    @Autowired
    private OSSAliyunFileStorageService ossAliyunFileStorageService;

    @Autowired
    private ITbSkuService tbSkuService;

    @PostMapping("/fileUpload")
    @ApiOperation("图片上传")
    public String fileUpload(@RequestParam(value = "fileName") MultipartFile file) throws IOException {
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        return ossAliyunFileStorageService.store(extName, file.getInputStream());
    }

    @PostMapping
    @ApiOperation("新增商品")
    public Boolean addSku(@RequestBody SkuDto skuDto){
       return tbSkuService.save(BeanUtil.toBean(skuDto, TbSku.class));
    }

    @GetMapping("/search")
    @ApiOperation("商品搜索")
    public PageResponse<SkuVo> getByPage(@RequestParam Integer pageIndex, @RequestParam Integer pageSize, @RequestParam(required = false) String skuName){
       return tbSkuService.getByPage(pageIndex,pageSize,skuName);
    }

    @PutMapping("/{skuId}")
    @ApiOperation("修改商品")
    public Boolean updateSku(@PathVariable String skuId, @RequestBody SkuDto skuDto){
        return tbSkuService.updateById(skuId,skuDto);
    }
}
